<meta charset="<?= getMessageByKey("default.charset") ?>">
<div class="maincommon">

  <?php

  if (!init()) {
    redirect("?action=login");
  }

  include_once("profile_menu.php");
  ?>

  <div class="markall" onclick="location.href='?action=markall'">
    <?= getMessageByKey("button.user.profile.sections.mark.all") ?>
  </div>

  <?php

  $topicSet = getTopics($_SESSION['id']);

  if ($topic = mysql_fetch_assoc($topicSet)) {
    $section = "";


    do {
      if ($topic['section'] != $section) {
        ?>
        <div class="profilesectionheader" style="margin-bottom: 25px;">
          <?= strtoupper($topic['section']) ?>
        </div>
        <?php
        $section = $topic['section'];
      }
      ?>

      <div class="topic">
        <div class="header" style="<?php if ($topic['uread'] == 0) {
          echo "background-color: rgba(255, 127, 036, 1);";
        } ?>" onclick="location.href='?action=topicchkread&tid=<?= $topic['id'] ?>'">
          <?= strtoupper($topic['header']) ?>
        </div>
        <div class="info">
          <div class="td">
            <?= getMessageByKey("sections.author") ?> <?= $topic['author'] ?><br>
            <?= getMessageByKey("section.create.date") ?> <?= $topic['date'] ?>
          </div>
          <div class="td">
            <?= getMessageByKey("section.create.time") ?> <?= $topic['time'] ?><br>
            <?= getMessageByKey("section.messages") ?> <?php $tmp = countTopicMessages($topic['id']);
            echo $tmp ?>
          </div>
          <div class="td">
            <?= getMessageByKey("section.group.available") ?> <?= $topic['agroup'] ?><br>
            <?= getMessageByKey("section.pages") ?>
            <div class="pages">
              <?php
              $pages = pagesFromMessages($tmp);
              $pagesStart = 0;
              $pagesEnd = 0;
              $page = 0;

              if ($pages <= 5) {
                $pagesStart = 1;
                $pagesEnd = $pages;
              } else if ($pages >= 5) {
                $pagesStart = 1;
                $pagesEnd = 5;
              }
              if ($pages == 0) {
                echo $pages;
              }

              if ($page > 1) {
                ?>

                <div class="page"
                     onclick="location.href='?action=topicchkread&tid=<?= $topic['id'] ?>&page=1'">
                  <<
                </div>
              <?php
              }
              for ($i = $pagesStart; $i <= $pagesEnd; $i++) {
                ?>
                <div class="page"
                     onclick="location.href='?action=topicchkread&tid=<?= $topic['id'] ?>&page=<?= $i ?>'">
                  <?= $i ?>
                </div>
              <?php
              }
              if ($page != $pages) {
                ?>
                <div class="page"
                     onclick="location.href='?action=topicchkread&tid=<?= $topic['id'] ?>&page=<?= $pages ?>'">
                  >>
                </div>
              <?php
              }
              ?>
            </div>
          </div>
        </div>
        <div class="lastmsgdiv">
          <?php
          $lastCommentsSet = getLastComments($topic['id']);
          while ($lastComment = mysql_fetch_assoc($lastCommentsSet)) {
            ?>
            <div class="td">
              <div class="author">
                <?= getMessageByKey("section.author") ?> <?= $lastComment['author'] ?><br>
                <?= getMessageByKey("general.date") ?> <?= $lastComment['date'] ?><br>
                <?= getMessageByKey("general.time") ?> <?= $lastComment['time'] ?>
              </div>
              <div class="content">
                <?= htmlspecialchars(getTextPart($lastComment['content'], 200)) ?>
              </div>
            </div>
          <?php
          }
          ?>
        </div>
        <div class="buttonsdiv" onclick="$('#checktopicdrop<?= $topic['id'] ?>').slideDown('slow')">
          <div class="button">
            <?= getMessageByKey("button.user.section.drop") ?>
          </div>
        </div>
        <div id="checktopicdrop<?= $topic['id'] ?>" class="askbuttonsdiv">
          <?= getMessageByKey("user.section.drop.ask") ?>
          <div class="button" onclick="location.href='?action=delsubscr&tid=<?= $topic['id'] ?>'">
            <?= getMessageByKey("button.general.continue") ?>
          </div>
          <div class="button" onclick="$('#checktopicdrop<?= $topic['id'] ?>').slideUp('slow')">
            <?= getMessageByKey("general.cancel") ?>
          </div>
        </div>
      </div>
    <?php

    } while ($topic = mysql_fetch_assoc($topicSet));
  }


  ?>
</div>